package com.mright.controller.base;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.mright.common.domain.PageData;
import com.mright.common.util.Logger;
import com.mright.common.util.Page;
import com.mright.common.util.UuidUtil;
import com.mright.entity.SysUser;

import javax.servlet.http.HttpServletRequest;

public class BaseController {

    protected Logger logger = Logger.getLogger(this.getClass());

    private static final long serialVersionUID = 6357869213649815390L;

    /** new PageData对象
     * @return
     */
    public PageData getPageData(){
        return new PageData(this.getRequest());
    }

    /**得到ModelAndView
     * @return
     */
//    public ModelAndView getModelAndView(){
//        return new ModelAndView();
//    }

    /**得到request对象
     * @return
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**得到32位的uuid
     * @return
     */
    public String get32UUID(){
        return UuidUtil.get32UUID();
    }

    /**得到分页列表的信息
     * @return
     */
    public Page getPage(){
        return new Page();
    }

	public static Subject getSubject() {
		return SecurityUtils.getSubject();
	}

    protected SysUser getCurrentUser() {
		return (SysUser) getSubject().getPrincipal();
	}
	
	protected Session getSession() {
		return getSubject().getSession();
	}

	protected Session getSession(Boolean flag) {
		return getSubject().getSession(flag);
	}

	protected void login(AuthenticationToken token) {
		getSubject().login(token);
	}
    
    public static void logBefore(Logger logger, String interfaceName){
        logger.info("");
        logger.info("start");
        logger.info(interfaceName);
    }

    public static void logAfter(Logger logger){
        logger.info("end");
        logger.info("");
    }
}
